package com.heima.feign.config;

import com.heima.common.util.LoginUserInfo;
import com.heima.common.util.UserInfoThreadLocal;
import com.heima.feign.fallback.StaticFeignClientFallback;
import feign.Logger;
import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeignConfiguration {

    @Bean
    public Logger.Level loggerLevel() {
        return Logger.Level.FULL;
    }

    @Bean
    public StaticFeignClientFallback staticFeignClientFallback() {
        return new StaticFeignClientFallback();
    }

    @Bean
    public RequestInterceptor requestInterceptor() {
        return requestTemplate -> {
            LoginUserInfo loginUserInfo = UserInfoThreadLocal.get();
            if (loginUserInfo != null) {
                requestTemplate.header("userId", loginUserInfo.getId().toString());
            } else {
                requestTemplate.header("userId", "0");
            }
        };
    }
}
